#pragma once
#include <iostream>
#include <memory>
#include <string>
#include <jsoncpp/json/json.h>

static const std::string message_sep = " ";
static const std::string package_sep = "\n";
#define SelfDefine 1
std::string EnCode(const std::string &message) // 将有效载荷封装上报头 //例如: "5\n1 + 2\n"
{
    std::cout << "EnCode::message: " << message << std::endl;
    return std::to_string(message.size()) + package_sep + message + package_sep;
}
std::string DeCode(std::string &package) // 从package里分离出message,//package传进来不一定decode成功
{
    int package_sep_pos = package.find(package_sep);
    if (package_sep_pos == std::string::npos)
        return std::string(); // len都不完整
    std::string s_len = package.substr(0, package_sep_pos);
    std::cout << "DeCode::s_len: " << s_len << std::endl;    //Debug
    // std::string s_len = package.substr(package_sep_pos);     //Debug：substr写错了
    int len = std::stoi(s_len);
    // int total = len + std::stoi(s_len) + 2 * package_sep.size(); //Debug:s_len.size()才对
    int total = len + s_len.size() + 2 * package_sep.size(); // Debug:s_len.size()才对

    if (package.size() < len)
        return std::string(); // 报文不完整

    // 报文完整
    std::string message = package.substr(total - len - 1, len);
    package.erase(0, total);
    return message;
}
class Request
{
public:
    Request()
    {
    }
    Request(int x, int y, char oper)
        : _x(x), _y(y), _oper(oper)
    {
    }

    void Inc()
    {
        _x++;
        _y++;
    }
    void Debug()
    {
        std::cout << _x << " " << _oper << " " << _y << std::endl;
    }
    std::string Serialize() //  目的："_x _oper _y"         //合法格式就是只有一个空格
    {
#ifdef SelfDefine
        return std::to_string(_x) + message_sep + _oper + message_sep + std::to_string(_y);
#else
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;
        Json::FastWriter writer;
        return writer.write(root);
#endif
    }
    bool DeSerialize(const std::string &message) // 填充成员变量
    {
#ifdef SelfDefine

        int left_sep_pos = message.find(message_sep);
        if (left_sep_pos == std::string::npos)
            return false; // 没找到返回空串
        int right_sep_pos = message.rfind(message_sep);
        if (right_sep_pos == std::string::npos)
            return false; // 没找到返回空串
        if (left_sep_pos == right_sep_pos)
            return false; //"1 +2"

        if (right_sep_pos - left_sep_pos != 2)
            return false; //"1 +  2"
        // 已经都对了，检测x和y用stoi
        _x = std::stoi(message.substr(0, left_sep_pos));
        _y = std::stoi(message.substr(right_sep_pos));
        _oper = message[left_sep_pos + 1];
        return true;
#else
        Json::Value root;
        Json::Reader reader;
        bool ok = reader.parse(message, root);
        if(ok)
        {
            _x = root["x"].asInt();
            _y = root["y"].asInt();
            _oper = root["oper"].asInt();
        }
        return ok;
#endif
    }
    int Getx() { return _x; }
    int Gety() { return _y; }
    char Getoper() { return _oper; }

private:
    int _x;
    int _y;
    char _oper;
};

class Response
{
public:
    Response()
    {
    }
    Response(int result, int state = 0)
        : _result(result), _state(state) //难怪这个state会不对啊，像没初始化一样
    {
    }
    void SetState(int state)
    {
        _state = state;
    }
    int GetState()
    {
        return _state;
    }
    std::string Serialize() //  目的："_x _oper _y"         //合法格式就是只有一个空格
    {
#ifdef SelfDefine

        return std::to_string(_result) + message_sep + std::to_string(_state);
#else
        Json::Value root;
        root["result"] = _result;
        root["state"] = _state;
        Json::FastWriter writer;
        return writer.write(root);
#endif
    }
    bool DeSerialize(const std::string &message) // 填充成员变量
    {
#ifdef SelfDefine

        int left_sep_pos = message.find(message_sep);
        if (left_sep_pos == std::string::npos)
            return false; // 没找到返回空串
        int right_sep_pos = message.rfind(message_sep);
        if (right_sep_pos == std::string::npos)
            return false; // 没找到返回空串
        if (left_sep_pos != right_sep_pos)
            return false; // 空格多了

        // 已经都对了
        _result = std::stoi(message.substr(0, left_sep_pos));
        _state = std::stoi(message.substr(left_sep_pos + 1));
        return true;
#else
        Json::Value root;
        Json::Reader reader;
        bool ok = reader.parse(message, root);
        if(ok)
        {
            _result = root["result"].asInt();
            _state = root["state"].asInt();
        }
        return ok;
#endif
    }
private:
    int _result;
    int _state;
};

class Factory // 建造者模式
{
public:
    static std::shared_ptr<Request> BulidRequest()      //直接去类里拿
    {
        return std::make_shared<Request>();
    }
    static std::shared_ptr<Request> BulidRequest(int x, int y, char oper)
    {
        return std::make_shared<Request>(x, y, oper);
    }
    static std::shared_ptr<Response> BulidResponse()
    {
        return std::make_shared<Response>();
    }
    static std::shared_ptr<Response> BulidResponse(int result, int state)
    {
        std::cout << "BulidResponse::state: " << state << std::endl;
        return std::make_shared<Response>(result, state);
    }
};